﻿using System;
using System.Collections.Generic;

namespace Xcapp
{
    /// <summary>
    /// Provides operation arguments, and arguments parsing logic.
    /// </summary>
    public static class OperationArgs
    {
        private static ArgDescriptions[] _argDescriptions;

        /// <summary>
        /// Parses the specified arguments.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns></returns>
        public static bool Parse(string[] args)
        {
            if (args == null || args.Length == 0) { return false; }

            bool result = true;
            int mandatoryArgs = 1;

            for (int i = 0; i < args.Length; i++)
            {
                if ((args[i].Substring(0, 1).Equals("/", StringComparison.OrdinalIgnoreCase) || args[i].Substring(0, 1).Equals("-", StringComparison.OrdinalIgnoreCase)) && args[i].Length > 1)
                {
                    args[i] = args[i].Substring(1);
                }

                switch (args[i].ToUpper())
                {
                    case "SSL":
                        if (Key == null)
                        {
                            Key = args[i];
                            mandatoryArgs--;
                        }

                        break;
                    case "CSL":
                        if (Key == null) { Key = args[i]; }
                        break;
                    case "ID":
                        User = args[++i];
                        mandatoryArgs--;
                        break;
                    case "MI":
                        AllowMultipleInstances = true;
                        break;
                }
            }

            if (mandatoryArgs > 0) { result = false; }

            return result;
        }

        /// <summary>
        /// Gets a value indicating whether {CC2D43FA-BBC4-448A-9D0B-7B57ADF2655C}[allow multiple instances].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [allow multiple instances]; otherwise, <c>false</c>.
        /// </value>
        public static bool AllowMultipleInstances { get; private set; }

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <value>
        /// The user.
        /// </value>
        public static string User { get; private set; }

        /// <summary>
        /// Gets the key.
        /// </summary>
        /// <value>
        /// The key.
        /// </value>
        public static string Key { get; private set; }

        /// <summary>
        /// Gets the arguments descriptions.
        /// </summary>
        /// <value>
        /// The arguments descriptions.
        /// </value>
        public static IEnumerable<ArgDescriptions> ArgumentsDescriptions
        {
            get
            {
                if (_argDescriptions == null)
                {
                    _argDescriptions = new ArgDescriptions[]
                        {
                            new ArgDescriptions("id", "User ID", "some other description", "some more description"),
                            new ArgDescriptions("mi", "Allow multiple symultaneously instances of the application"),
                            new ArgDescriptions("pw","Password", "some password", "your password","correct password", "please")
                        };
                }

                return _argDescriptions;
            }
        }
    }
}